package com.itheima.web;

import com.itheima.entity.Result;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryWeb {
    //注入业务层对象
    @Autowired
    private CategoryService categoryService;

    //查询所有用户
    @GetMapping("/findAll")
    public Result<List<Category>> findAll(){
        return categoryService.findAll();
    }

    //添加数据
    @PostMapping("/add")
    public Result<List<Category>> add(Category category){

        return categoryService.add(category);
    }


    //get分页查询方式
    @GetMapping("/findPage/search/{page}/{size}")
    public Result<Object> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size){

        return categoryService.findPage(page,size);
    }

    //按id查询数据
    @GetMapping("/{id}")
    public Result<Object> findById(@PathVariable("id")Integer id){

        return categoryService.findById(id);
    }


    @PutMapping("/{id}")
    public Result<Object> update(@PathVariable("id")Integer id,Category category){
        category.setId(id);
        return categoryService.update(category);
    }

    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable("id")Integer id){
        return categoryService.delete(id);
    }
}
